package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Emp;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper,Dept> implements DeptService {

    @Autowired
    private EmpMapper empMapper;

    /**
     * 判断部门中是否还有员工
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {

        //创建对象
        Long count = empMapper.selectCount(new LambdaQueryWrapper<Emp>().eq(Emp::getDeptId, id));

        //判断是否还有员工
        if (count > 0) {

            //还有员工，抛出异常
            throw new SecurityException("该部门还有员工，不能删除");
        }

        //没有员工，直接删除
        removeById(id);

    }
}
